package com.changgou.search.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping     //这里因为是get请求,所以参数会在路径上拼接,里面的值会进行编码和解码的,编码和解码后的一些值就不是我们期望的了,所以这里要对特殊字符处理
    public Result search(@RequestParam Map<String,String> searchmap){
        //特殊符号处理
        this.handleSearchMap(searchmap);
        Map search = searchService.search(searchmap);//这里面包含数据集合和总记录数总页数
        return new Result(true, StatusCode.OK,"多条件查询成功",search);
    }

    private void handleSearchMap(Map<String, String> searchmap) {
        Set<Map.Entry<String, String>> entries = searchmap.entrySet();
        for (Map.Entry<String, String> entry : entries) {//对每一个键值对处理
             if (entry.getKey().startsWith("spec_")){
                 searchmap.put(entry.getKey(),entry.getValue().replace("+","%2B"));
             }
        }
    }
}
